<template>
  <div class="login-container">
    <!-- 左侧：系统介绍 -->
    <div class="login-left">
      <div class="left-content">
        <div class="logo-section">
          <n-icon size="80" color="#ffffff">
            <RestaurantOutline />
          </n-icon>
          <h1 class="system-title">面包房管理系统</h1>
          <p class="system-subtitle">Bakery Management System</p>
        </div>
        
        <div class="features">
          <div class="feature-item">
            <n-icon size="24" color="#ffffff">
              <CheckmarkCircleOutline />
            </n-icon>
            <span>高效的员工与权限管理</span>
          </div>
          <div class="feature-item">
            <n-icon size="24" color="#ffffff">
              <CheckmarkCircleOutline />
            </n-icon>
            <span>实时库存监控与预警</span>
          </div>
          <div class="feature-item">
            <n-icon size="24" color="#ffffff">
              <CheckmarkCircleOutline />
            </n-icon>
            <span>完整的销售记录追溯</span>
          </div>
          <div class="feature-item">
            <n-icon size="24" color="#ffffff">
              <CheckmarkCircleOutline />
            </n-icon>
            <span>智能数据统计分析</span>
          </div>
        </div>
        
        <div class="copyright">
          <p>© 2025 Bakery System. All rights reserved.</p>
        </div>
      </div>
    </div>
    
    <!-- 右侧：登录表单 -->
    <div class="login-right">
      <div class="login-form-wrapper">
        <div class="form-header">
          <h2>欢迎回来</h2>
          <p>请登录您的账号</p>
        </div>
        
        <n-form ref="formRef" :model="formData" :rules="rules" size="large">
          <n-form-item path="account">
            <n-input 
              v-model:value="formData.account" 
              placeholder="请输入账号"
              clearable
            >
              <template #prefix>
                <n-icon color="#18A058"><PersonOutline /></n-icon>
              </template>
            </n-input>
          </n-form-item>
          
          <n-form-item path="password">
            <n-input 
              v-model:value="formData.password" 
              type="password" 
              placeholder="请输入密码"
              show-password-on="click"
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <n-icon color="#18A058"><LockClosedOutline /></n-icon>
              </template>
            </n-input>
          </n-form-item>
          
          <n-form-item>
            <n-button 
              type="primary" 
              block 
              size="large" 
              :loading="loading"
              @click="handleLogin"
              style="margin-top: 8px;"
            >
              <template #icon>
                <n-icon><LogInOutline /></n-icon>
              </template>
              登录系统
            </n-button>
          </n-form-item>
        </n-form>
        
        <n-divider style="margin: 24px 0;" />
        
        <div class="demo-info">
          <n-alert type="info" :bordered="false">
            <template #icon>
              <n-icon><InformationCircleOutline /></n-icon>
            </template>
            <div style="font-size: 13px;">
              <p style="margin-bottom: 4px;"><strong>默认管理员账号</strong></p>
              <p>账号：<code>admin</code> | 密码：<code>123456</code></p>
            </div>
          </n-alert>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { login } from '@/api/employee'
import { NForm, NFormItem, NInput, NButton, NDivider, NIcon, NAlert, useMessage } from 'naive-ui'
import { 
  PersonOutline, LockClosedOutline, RestaurantOutline, 
  CheckmarkCircleOutline, LogInOutline, InformationCircleOutline 
} from '@vicons/ionicons5'

const router = useRouter()
const userStore = useUserStore()
const message = useMessage()

const formRef = ref(null)
const loading = ref(false)
const formData = ref({
  account: '',
  password: ''
})

const rules = {
  account: { required: true, message: '请输入账号', trigger: 'blur' },
  password: { required: true, message: '请输入密码', trigger: 'blur' }
}

async function handleLogin() {
  try {
    await formRef.value?.validate()
    loading.value = true
    
    const res = await login(formData.value)
    const { token, ...userInfo } = res.data
    
    userStore.setToken(token)
    userStore.setUserInfo(userInfo)
    
    message.success('登录成功')
    router.push('/')
  } catch (error) {
    if (error.message) {
      message.error(error.message)
    }
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* 左侧：系统介绍 */
.login-left {
  flex: 1;
  background: linear-gradient(135deg, #18A058 0%, #0f7d42 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.login-left::before {
  content: '';
  position: absolute;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 1px, transparent 1px);
  background-size: 50px 50px;
  animation: backgroundMove 20s linear infinite;
}

@keyframes backgroundMove {
  0% { transform: translate(0, 0); }
  100% { transform: translate(50px, 50px); }
}

.left-content {
  position: relative;
  z-index: 1;
  color: #ffffff;
  text-align: center;
  padding: 60px 80px;
  max-width: 600px;
}

.logo-section {
  margin-bottom: 60px;
  animation: fadeInDown 0.8s ease-out;
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.system-title {
  font-size: 42px;
  font-weight: 700;
  margin: 20px 0 10px;
  letter-spacing: 2px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

.system-subtitle {
  font-size: 18px;
  opacity: 0.9;
  font-weight: 300;
  letter-spacing: 1px;
}

.features {
  text-align: left;
  margin: 0 auto;
  max-width: 400px;
  animation: fadeInUp 0.8s ease-out 0.2s backwards;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  font-size: 16px;
  opacity: 0.95;
  transition: all 0.3s ease;
}

.feature-item:hover {
  opacity: 1;
  transform: translateX(10px);
}

.copyright {
  margin-top: 80px;
  font-size: 14px;
  opacity: 0.7;
}

/* 右侧：登录表单 */
.login-right {
  flex: 0 0 520px;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  box-shadow: -5px 0 20px rgba(0, 0, 0, 0.05);
}

.login-form-wrapper {
  width: 100%;
  max-width: 400px;
  animation: fadeIn 0.6s ease-out 0.3s backwards;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.form-header {
  margin-bottom: 40px;
  text-align: center;
}

.form-header h2 {
  font-size: 32px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.form-header p {
  font-size: 15px;
  color: #666;
}

.demo-info {
  margin-top: 20px;
}

.demo-info code {
  background: #f5f5f5;
  padding: 2px 8px;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  color: #18A058;
  font-weight: 600;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .login-left {
    flex: 0 0 45%;
  }
  
  .left-content {
    padding: 40px;
  }
  
  .system-title {
    font-size: 36px;
  }
  
  .features {
    max-width: 100%;
  }
}

@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }
  
  .login-left {
    flex: 0 0 auto;
    padding: 40px 20px;
  }
  
  .left-content {
    padding: 20px;
  }
  
  .system-title {
    font-size: 28px;
  }
  
  .features,
  .copyright {
    display: none;
  }
  
  .login-right {
    flex: 1;
    padding: 20px;
  }
}
</style>

